package com.xzy.seckill;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xzy.seckill.common.result.R;
import com.xzy.seckill.controller.UserController;
import com.xzy.seckill.pojo.dto.LoginDTO;
import com.xzy.seckill.pojo.dto.RegisterDTO;
import com.xzy.seckill.pojo.entity.SeckillGoods;
import com.xzy.seckill.pojo.entity.User;
import com.xzy.seckill.pojo.vo.LoginVO;
import com.xzy.seckill.pojo.vo.SeckillGoodDetailVO;
import com.xzy.seckill.service.OrderService;
import com.xzy.seckill.service.SeckillGoodsService;
import com.xzy.seckill.service.SeckillOrderService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Map;

@SpringBootTest
public class cheatUSer {
    @Autowired
    UserController userController;

    @Autowired
    SeckillOrderService seckillOrderService;

    @Autowired
    OrderService orderService;

    @Autowired
    SeckillGoodsService seckillGoodsService;

    @Resource
    RedisTemplate redisTemplate;

    //创建用户并登录生成token
    @Test
    void creat() throws IOException {
        int num = 5000;
        ArrayList<RegisterDTO> registerDTOS = new ArrayList<>(num);
        File file = new File("D:\\config.txt");
        FileWriter fileWritter = new FileWriter("D:\\config.txt",true);
        if(!file.exists()){
            file.createNewFile();
        }
        for (int i = 0; i < num; i++) {
            RegisterDTO registerDTO = new RegisterDTO();
            registerDTO.setPhone(String.valueOf(i));
            registerDTO.setNickname("test");
            registerDTO.setPassword("123456");
            userController.register(registerDTO);
            R r = userController.doLogin(new LoginDTO(String.valueOf(i), "123456"));
            LoginVO user = (LoginVO)r.getData().get("user");  //想办法将这个写到文件里面
            String token = user.getToken();
            fileWritter.write(token+",\r\n");
        }
        fileWritter.close();

    }

    @Test
    void login() throws IOException {
        int num = 5000;
        ArrayList<RegisterDTO> registerDTOS = new ArrayList<>(num);
        File file = new File("D:\\config.txt");
        FileWriter fileWritter = new FileWriter("D:\\config.txt",true);
        if(!file.exists()){
            file.createNewFile();
        }
        for (int i = 0; i < num; i++) {

            R r = userController.doLogin(new LoginDTO(String.valueOf(i), "123456"));
            LoginVO user = (LoginVO)r.getData().get("user");  //想办法将这个写到文件里面
            String token = user.getToken();
            fileWritter.write(token+",\r\n");
        }
        fileWritter.close();

    }
    @Test
    void test(){

        SeckillGoodDetailVO seckillGoodDetailVO = new SeckillGoodDetailVO();
        seckillGoodDetailVO.setEndDate(LocalDateTime.now());
        seckillGoodDetailVO.setGoodImg("aaa");
        seckillGoodDetailVO.setGoodsPrice(new BigDecimal("1.1111111111111111111111"));


        String s = JSONUtil.toJsonStr(seckillGoodDetailVO);
        SeckillGoodDetailVO seckillGoodDetailVO1 = JSONUtil.toBean(s, SeckillGoodDetailVO.class);
        System.out.println(seckillGoodDetailVO1);


    }

    @Test
    void prepare(){
        //清除订单
        seckillOrderService.getBaseMapper().delete(null);
        orderService.getBaseMapper().delete(null);
        //设置redis
        redisTemplate.opsForValue().set( "sec:product:stock:1",100);
        //设置数量
        seckillGoodsService.update(
                new UpdateWrapper<SeckillGoods>().eq("goods_id",1).
                        setSql("stock_count=100"));
    }




}
